package be.scond.cartpool;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.*;
import be.scond.cartpool.API.ShoppingListApi;
import be.scond.cartpool.exceptions.NoShoppingListFoundException;
import be.scond.cartpool.model.Order;

public class ReturnOrdersServlet extends HttpServlet {
	
	static final long serialVersionUID = 1;
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/xml");		
		
		String keyString = req.getParameter("key");

		String respons = "<data>";	
		
		try {
			List<Order> orders = ShoppingListApi.getOrders(keyString);				
			respons += "<succes>"+orders.size()+" orders where found</succes>";			
			for (Order order : orders) {
				respons += order.toXmlString();
			}
		} catch (NoShoppingListFoundException e) {
			respons += "<error>ShoppingList with key "+keyString+" to return orders from is not found</error>";
		}
			
		respons += "</data>";

		resp.getWriter().print(respons);
	}
}